package com.jacobson.huobi.common.expansion

import android.app.Activity
import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager

fun ImageView.load(applyOptions: (request: RequestManager) -> RequestBuilder<Drawable>) {
    setImageBitmap(null)
    withGlide(context)?.let {
        applyOptions(it).into(this)
    }
}

private fun withGlide(context: Context?): RequestManager? {
    if (context == null) {
        return null
    }
    return (context as? Activity)?.let { Glide.with(it) } ?: Glide.with(context)
}